#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
#include <iostream>

#define MAX_NUM 3

uv_loop_t *loop;
int count = 0;

void idle_cb(uv_idle_t *handle) {
  count++;
  printf("idle handle callback, count = %d\n", count);
  if (count >= MAX_NUM) {
    printf("idle handle stop, count = %d\n", count);
    uv_stop(loop);
  }
}

void prepare_cb(uv_prepare_t *handle) {
  printf("prepare handle callback\n");
}

void check_cb(uv_check_t *check) {
  printf("check handle callback\n");
}

int main() {
  loop = (uv_loop_t *) malloc(uv_loop_size());
  uv_loop_init(loop);
  // loop = uv_default_loop();

  uv_idle_t idle;
  uv_prepare_t prepare;
//  uv_check_t check;

  uv_idle_init(loop, &idle);
  uv_idle_start(&idle, idle_cb);
//
  uv_prepare_init(loop, &prepare);
  uv_prepare_start(&prepare, prepare_cb);
//
//  uv_check_init(loop, &check);
//  uv_check_start(&check, check_cb);

  uv_run(loop, UV_RUN_DEFAULT);

  if (UV_EBUSY == uv_loop_close(loop)) {
    std::cout << "busy" << std::endl;
  }

  return 0;
}
